package com.gitee.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具类
 * 
 * @author 赵炎
 * @version [V1.00, 2017年10月30日]
 * @see [相关类/方法]
 * @since V1.00
 */
public class DateUtil
{
    public static final String DEFAULT_PATTERN = "yyyyMMdd";
    
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    
    /**
     * FIRST_DAY_OF_CN :中国每个星期滴一天 “0” 星期一
     */
    public static final String FIRST_DAY_OF_CN = "0";
    
    /**
     * FIRST_DAY_OF_WESTERN:西方每个星期滴一天 “1” 星期天
     */
    public static final String FIRST_DAY_OF_WESTERN = "1";
    
    public static final String YYYY_MM_DD_HHMMSS = "yyyy-MM-dd HH:mm:ss";
    
    /**
     * 根据日期类获取当前系统时间
     * 
     * @return 当前日期
     */
    public static Date getCurrentDate()
    {
        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }
    
    /**
     * 以固定pattern格式化日期
     * 
     * @param date
     * @param pattern
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String format(final Date date, String pattern)
    {
        if (StringUtils.isBlank(pattern))
        {
            pattern = DEFAULT_PATTERN;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }
    
    /**
     * 格式化日期 默认为yyyyMMdd
     * 
     * @param date
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String format(final Date date)
    {
        return format(date, null);
    }
    
    /**
     * 获取默认的时间 此为当前时间
     * 默认为yyyyMMdd
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static String format()
    {
        return format(getCurrentDate());
    }
    
    public static Date parse(final String date, String pattern)
    {
        if (StringUtils.isBlank(date))
        {
            throw new IllegalArgumentException("date is illegal");
        }
        if (StringUtils.isBlank(pattern))
        {
            pattern = DEFAULT_PATTERN;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try
        {
            return sdf.parse(date);
        }
        catch (ParseException e)
        {
            return null;
        }
        
    }
    
    public static Date parse(final String date)
    {
        return parse(date, null);
    }
    
    /**
     * 获取每周开始的第一天，默认西方为星期天为第一天
     * 在中国，星期一为每周的第一天
     * 
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static Date firstDayOfWeek(final String local)
    {
        Calendar calendar = Calendar.getInstance();
        if (FIRST_DAY_OF_CN.equals(local))
        {
            //calendar.setFirstDayOfWeek(Calendar.MONDAY);
            // 需要设置参数
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        }
        else
        {
            //calendar.setFirstDayOfWeek(Calendar.SUNDAY);
            // 需要设置此参数
            calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        }
        
        return calendar.getTime();
    }
    
    /**
     * 获取当前日期的为当前月的第几周
     * 
     * @param date 当前日
     * @param local “0”：中国；“1”：西方
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static int getWeekOfMonth(final Date date, final String local)
    {
        Calendar calendar = Calendar.getInstance();
        if (FIRST_DAY_OF_CN.equals(local))
        {
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
        }
        else
        {
            calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        }
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_MONTH);
    }
    
    /**
     * 获取今日为所处月份的第几周
     * 
     * @param local “0”：中国；“1”：西方
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static int getWeekOfMonth(final String local)
    {
        Calendar calendar = Calendar.getInstance();
        if (FIRST_DAY_OF_CN.equals(local))
        {
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
        }
        else
        {
            calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        }
        return calendar.get(Calendar.WEEK_OF_MONTH);
    }
    
    /*
     * // TODO the DateUtil is continue...
     */
    
    public static Date before()
    {
        return null;
    }
    
    public static Date after()
    {
        return null;
    }
    
    /** 
     * 获取当前日期这周最后一天
     * @return
     * @see [类、类#方法、类#成员]
     */
    public static Date getLastDayOfWeek(final String local) 
    {
        Calendar calendar = Calendar.getInstance();
        if (FIRST_DAY_OF_CN.equals(local))
        {
            calendar.setFirstDayOfWeek(Calendar.MONDAY);
        }
        else
        {
            calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        }
        calendar.set(Calendar.DAY_OF_WEEK,6);
        return calendar.getTime();
    }
}
